# Simple script to run in place of real compiler; for spec testing only
class J750_PatCompiler
  def initialize
    @atp = nil
    @pat = nil
  end

  def parse_args(args)
    args.each do |arg|
      if arg[0] != '-'
        @atp = Pathname.new(arg)
      elsif match = arg.match(/^-output:(\S*)/)
        @pat = Pathname.new(match.captures[0])
      end
    end
  end

  def compile
    # puts 'J750 Pattern Compiler - fake version' 
    File.open(@pat, 'w') { |f| f.write(file_contents) }                  # PAT file

    logfile = Pathname.new(@atp.to_s.gsub('.atp', '.log'))
    File.open(logfile, 'w') { |f| f.write(file_contents(log: true)) }   # log file
  end

  def file_contents(options = {})
    options[:log] = false if options[:log].nil?
    contents = 'generated by J750 Pattern Compiler - fake version' 
    options[:log] ? 'Log file ' + contents : 'PAT file ' + contents
  end
end

if __FILE__ == $0
  apc = J750_PatCompiler.new
  apc.parse_args(ARGV)
  apc.compile
end

